<template>
    <div :class="scrollIng ? 'header scroll-header' : 'header'">
        <img src="./img/logo.png" alt="" />
        <div class="nav-box">
            <router-link
                v-for="item in navArr"
                :key="item.link"
                :to="item.link"
                v-slot="{ href, route, navigate, isActive, isExactActive }"
            >
                <li
                    :class="[
                        isActive && 'router-link-active',
                        isExactActive && 'router-link-exact-active'
                    ]"
                >
                    <a :href="href" @click="navigate">{{ item.name }}</a>
                    <transition name="fade">
                        <div
                            v-show="isExactActive"
                            :class="isExactActive && 'active-line'"
                        ></div>
                    </transition>
                </li>
            </router-link>
            <el-dropdown
                class="dropdown-box"
                @command="handleCommand"
                trigger="click"
            >
                <span class="el-dropdown-link">
                    {{ $t("lang")
                    }}<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="zh">中文</el-dropdown-item>
                    <el-dropdown-item command="en">English</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
    </div>
</template>

<script>
import { mapState } from "vuex";
import Cookies from "js-cookie";
export default {
    name: "MHeader",
    components: {},
    data() {
        return {
            scrollIng: false
        };
    },

    computed: {
        navArr() {
            return [
                {
                    name: this.$t("navbar.home"),
                    link: "/"
                },
                {
                    name: this.$t("navbar.platform"),
                    link: "/platform"
                },
                {
                    name: this.$t("navbar.ad"),
                    link: "/ad"
                },
                {
                    name: this.$t("navbar.media"),
                    link: "/media"
                },
                {
                    name: this.$t("navbar.callme"),
                    link: "/callme"
                }
            ];
        },
        ...mapState({
            tabData: state => state.global.tabData
        })
    },
    mounted() {
        window.addEventListener("scroll", this.handleScroll);
    },
    methods: {
        handleCommand(lang) {
            Cookies.set("language", lang);
            this.$i18n.locale = lang;
        },
        handleScroll() {
            var scrollTop =
                window.pageYOffset ||
                document.documentElement.scrollTop ||
                document.body.scrollTop;
            if (scrollTop <= 10) {
                this.scrollIng = false;
            } else {
                this.scrollIng = true;
            }
        }
    }
};
</script>

<style lang="less" scoped>
.scroll-header {
    background-color: rgba(0, 0, 0, 0.5) !important;
}
.header {
    opacity: 0.9;
    display: flex;
    justify-content: space-between;
    height: 80px;
    background-color: #000000;
    align-items: center;
    padding: 0 250px;
    position: fixed;
    width: 100%;
    z-index: 1000;
    transition: all 0.5s;
    > img {
        height: 62px;
    }
    li {
        min-width: 100px;
        height: 100%;
        display: flex;
        align-items: center;
        flex-direction: column;
        justify-content: center;
        position: relative;
    }
}
.nav-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 900px;
    height: 100%;
}
.nav-box a {
    font-size: 18px;
    color: #fff;
    font-weight: 900;
    min-width: 100px;
    &:hover {
        color: @primary_color;
    }
}
.router-link-exact-active a {
    color: @primary_color;
}
.dropdown-box {
    margin-left: 30px;
    cursor: pointer;
    min-width: 50px;
}
.el-dropdown-link {
    color: #fff;
    font-weight: 800;
}

.active-line {
    width: 40px;
    height: 8px;
    border-radius: 4px;
    background-color: #0efffc;
    position: absolute;
    bottom: 0;
}
.fade-enter-active,
.fade-leave-active {
    transition: opacity 0.5s, width 0.5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
    width: 0;
}
</style>
